﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using System.Data.SqlClient;
using System.Text;
using System.Net.Mail;


public partial class mobile_hgconfirm : System.Web.UI.Page
{
    public string openid;
    public string enterid;
    public string productid;
    protected void Page_Load(object sender, EventArgs e)
    {
        openid = this.Request.Params["openid"].ToString();
        enterid = this.Request.Params["enterid"].ToString();
        productid = this.Request.Params["pid"].ToString();
        SqlConnection sc = new SqlConnection(ConfigurationManager.ConnectionStrings["wbkpt"].ConnectionString.ToString());
        sc.Open();
        SqlCommand get_name = new SqlCommand();
        get_name.CommandText="select PRODUCT_NAME from INFO_PRODUCT where PRODUCT_ID=@id";
        get_name.Connection = sc;
        get_name.Parameters.AddWithValue("@id",productid);
        object a=get_name.ExecuteScalar();
        if (a == null)//避免出错
        {
            sc.Close();

            Page.RegisterStartupScript("ss", "<script>alert('系统出错!')</script>");
            return;
        }
        else
        {//获取页面所需信息
            SqlCommand get_rate = new SqlCommand();
            get_rate.CommandText = "select EX_RATE from INFO_EXCHANGE where PRODUCT_ID=@id";
            get_rate.Connection = sc;
            get_rate.Parameters.AddWithValue("@id", productid);
            int rate = Convert.ToInt32(get_rate.ExecuteScalar().ToString());
            string name = a.ToString();
            label1.Text = "您即将确认换购" + name + "并使用" + rate + " 积分,请填写正确的收货地址";

        }
    }
    protected void button1_Click(object sender, EventArgs e)
    {
        if (textbox1.Text.Length != 6)
        {
            Page.RegisterStartupScript("ss", "<script>alert('邮政编码错误!')</script>");
            return;
        }
        if(textbox2.Text.Length==0)
        {
            Page.RegisterStartupScript("ss", "<script>alert('请填写地址!')</script>");
            return;
        }
        if (textbox3.Text.Length == 0)
        {
            Page.RegisterStartupScript("ss", "<script>alert('请填写收货人姓名!')</script>");
            return;
        }
        if (textbox4.Text.Length != 11)
        {
            Page.RegisterStartupScript("ss", "<script>alert('请填写正确手机号码!')</script>");
            return;
        }
        //四项验证通过后继续，发送邮件
        SqlConnection sc1 = new SqlConnection(ConfigurationManager.ConnectionStrings["wbkpt"].ConnectionString.ToString());
        sc1.Open();
        SqlCommand get_name = new SqlCommand();
        get_name.Connection = sc1;
        get_name.CommandText = "select IND_NAME from INFO_IND where IND_ID=(select IND_ID from IND_CARE where IND_OPENID=@oid and ENTER_ID=@eid)";
        get_name.Parameters.AddWithValue("@oid", openid);
        get_name.Parameters.AddWithValue("@eid", enterid);
        string u_name = get_name.ExecuteScalar().ToString();

        SqlCommand get_email = new SqlCommand();
        get_email.Connection = sc1;
        get_email.CommandText = "select IND_EMAIL from INFO_IND where IND_ID=(select IND_ID from IND_CARE where IND_OPENID=@oidd and ENTER_ID=@eidd)";
        get_email.Parameters.AddWithValue("@oidd", openid);
        get_email.Parameters.AddWithValue("@eidd", enterid);
        string email_a = get_email.ExecuteScalar().ToString();

      
       



        MailMessage msg = new MailMessage();
        msg.To.Add(email_a);
        msg.From = new MailAddress("wbktypt@qq.com", "微百科通用平台", System.Text.Encoding.UTF8);
        msg.Subject = "来自微百科通用平台-----请确认您的收货地址";//邮件标题 
        msg.SubjectEncoding = System.Text.Encoding.UTF8;//邮件标题编码 
        string cfurl="http://lcsy2008.eicp.net/wbkpro/mobile_hgconfirm2.aspx?indname="+Server.UrlEncode(u_name)+"&pid="+productid+"&pcode="+textbox1.Text+"&add="+Server.UrlEncode(textbox2.Text)+"&recname="+Server.UrlEncode(textbox3.Text)+"&recphone="+textbox4.Text+"&confirm=t";
        msg.Body = "尊敬的用户"+u_name+"您好，"+"\n"+
            "您所填写的地址为  "+textbox2.Text+"邮编为  "+textbox1.Text+
            "收货人姓名  "+textbox3.Text+"收货人手机  "+textbox4.Text+
            "\n"+"如果您不是平台用户或没有进行过任何类似操作请忽略此邮件。您不需要退订或进行其他进一步的操作。"+"\n"+
            "请点击以下地址跳转确认后我们就将为您发货" + "\n" +
            cfurl;//邮件内容 
        
        msg.BodyEncoding = System.Text.Encoding.UTF8;//邮件内容编码 
        msg.IsBodyHtml = false;//是否是HTML邮件 
        msg.Priority = MailPriority.High;//邮件优先级

        SmtpClient client = new SmtpClient();
        client.DeliveryMethod = SmtpDeliveryMethod.Network; //将smtp的出站方式设为 Network
        client.EnableSsl = false;//smtp服务器是否启用SSL加密
        client.Host = "smtp.qq.com";
        client.Port = 25; //指定 smtp 服务器的端口，默认是25
        client.Credentials = new System.Net.NetworkCredential("wbktypt@qq.com", "wbktypt");
        //注册的邮箱和密码 
        
        object userState = msg;
        try
        {
            client.Send(msg);
            Page.RegisterStartupScript("ss", "<script>alert('确认邮件已发送到您的邮箱，请及时查收确认!')</script>");
            return;
        }
        catch (Exception ex)
        {
            label1.Text = ex.StackTrace + ex.Message;
            Page.RegisterStartupScript("ss", "<script>alert('发送失败，请重试!')</script>");
            return;
           
        }
    }
}